package com.wulang.extension.listener;


import com.rabbitmq.client.Channel;
import com.wulang.extension.constants.Constants;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

import java.io.IOException;

@Component
public class AckListener {
    @RabbitListener(queues = Constants.ACK_QUEUE)
    public void ackListener(Message message, Channel channel) throws IOException {
        try {
            System.out.printf("接收到消息: %s, deliveryTag: %d \n", new String(message.getBody()), message.getMessageProperties().getDeliveryTag());
            //具体的业务逻辑处理
            int i = 3 / 0;
            System.out.println("业务逻辑处理成功");
            channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);
        } catch (Exception e) {
            channel.basicNack(message.getMessageProperties().getDeliveryTag(), false, true);
            throw new RuntimeException(e);
        }
        System.out.println("消息处理成功");
    }


}
